package com.example.demo.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.TestMapper;
import com.example.demo.service.TestService;
import com.example.demo.untils.SerializeUtil;
import org.apache.http.client.protocol.HttpClientContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liujiahao on 2019/4/22.
 */
@Service
@DS("slave_1")
public class TestServiceImpl  implements TestService{
    @Autowired
    TestMapper mapper;
    @Autowired
    StudentMapper stumapper;
    @Autowired
    JedisPool jedisPool;
    @Override
    public List findall() {
        Jedis jedis=jedisPool.getResource();
        List<String> list= mapper.findAll();
        jedis.select(4);
        Long  start= DateUtil.currentSeconds();
        for (String s : list) {
            jedis.lpush("ka",s);
        }
        Long  end= DateUtil.currentSeconds();
        System.out.println(end-start);
       /* Map<String,String> map=new HashMap<>();
        map.put("sessionId","321321321");
        map.put("xxxx","xxxxxx");
        jedis.hmset("cookie",map);*/
        //jedis.lpush("list","321321");
        /*for (Map<String, String> map : list) {
            jedis.hmset("list"+map.get("id").toString(),map);
        }*/

        return null;
    }
    public void findbyCondition(){
        Jedis jedis=jedisPool.getResource();
        jedis.select(4);
        Long  start= DateUtil.currentSeconds();
        List<String> list=jedis.lrange("ka",0,-1);
        for (int i = 0; i < 1000; i++) {
            for (String s : list) {
                if(s.equals("adsadsa")) System.out.println("yes");
            }
        }
        Long  end= DateUtil.currentSeconds();
        System.out.println("redis"+(end-start));
    }

    @Override
    public void findbyNO(String no) {
        Long  start= DateUtil.currentSeconds();
        for (int i = 0; i < 1000; i++) {
            mapper.findbyNO("adsadsa");
        }
        Long  end= DateUtil.currentSeconds();
        System.out.println("mysql"+(end-start));
    }
    @Transactional
    @DS("master")
    @Override
    public void insert() {
        HttpClientContext context=new HttpClientContext();
        stumapper.update();
        Jedis jedis=jedisPool.getResource();
        /*jedis.rpush("list","3333");
        jedis.rpush("list","2222");
        jedis.rpush("list".getBytes(),SerializeUtil.serialize(context));*/
        Transaction t=jedis.multi();
        t.rpush("list","3333");
        t.rpush("list","2222");
        if(true)throw new RuntimeException();
        try {

        }catch (Exception e){

        }
       /* System.out.println("开始休眠");
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束休眠");*/
        //t.rpush("list".getBytes(),SerializeUtil.serialize(context));
        t.exec();
    }

    public static void main(String[] args) {
        Long  start= DateUtil.currentSeconds();
        System.out.println(start);
        Long  end= DateUtil.currentSeconds();
    }
}
